
// 操作数组元素的方法： push pop shift unshift indexOf lastIndexOf includes find findIndex

let arr = [1, 44, 5, 65, 4];

arr.push(3);
console.log(arr);

console.log(arr.pop());
console.log(arr);

arr.shift()
console.log(arr);
arr.unshift(33);
console.log(arr);

console.log(arr.indexOf(5));
console.log(arr.lastIndexOf(5, 1));

console.log(arr.includes(5));

console.log('---------------');

// pop push shift unshift includes indexOf lastIndexOf find findIndex

console.log(arr.findIndex((element, index, arr) => {
  if(element == 5) {
    console.log(`this is ${element}, we can find it.`);
    return true;
  }
}));

// 操作整个数组的方法 map reduce slice concat filter splice sort flat join some
console.log('------ 操作整个数组的方法 --------');

console.log(arr.map((value, index) => {
  return value + index;
}))
console.log(arr.reduce((sum, value) => {
  console.log(sum);
  return sum + value;
}, 0));
console.log(arr.sort((a, b) => {
  return a - b;
})); // 原地修改

console.log(arr.slice(2, 3));
console.log([3, 3].concat(arr));

console.log('---', arr);
console.log(arr.splice(3, 0, 4, 5)); // 原地修改
console.log(arr);

// 原地修改 sort splice fill
// slice concat map reduce filter flat join 

arr.splice(3, 4);
console.log(arr);

console.log(arr.fill(3, 1));
console.log(arr);   

console.log(arr.join(''));

arr.push([3, 4, 5])
console.log(arr.flat(1));

console.log(arr.filter((value, index) => {
  return value === 3;
}));